package cn.schoolwow.quickdao.module.database.parent.flow.executor.common;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.domain.database.parent.RealTimePrintStatementOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.apache.commons.lang3.StringUtils;

public class RealtimePrintStatementCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);

        if(null==quickDAOConfig.logRecordOption.realTimePrintStatementOption){
            return;
        }
        String formatSQL = flowContext.checkData("formatSQL", String.class);
        int sqlLogType = flowContext.checkData("sqlLogType", int.class);

        RealTimePrintStatementOption realTimePrintStatementOption = quickDAOConfig.logRecordOption.realTimePrintStatementOption;
        if(null!=realTimePrintStatementOption.databaseStatementFilterType){
            //0:查询,1:更新
            switch (realTimePrintStatementOption.databaseStatementFilterType){
                case OnlyShowSelect:{
                    if(sqlLogType==1){
                        return;
                    }
                }
                case OnlyShowUpdate:{
                    if(sqlLogType==0){
                        return;
                    }
                }break;
            }
        }
        if(null!=realTimePrintStatementOption.tableNameList&&!realTimePrintStatementOption.tableNameList.isEmpty()){
            boolean contains = false;
            for(String tableName:realTimePrintStatementOption.tableNameList){
                if(formatSQL.contains(tableName)){
                    contains = true;
                    break;
                }
            }
            if(!contains){
                return;
            }
        }
        System.out.println(formatSQL);
    }

    @Override
    public String name() {
        return "实时打印SQL语句";
    }
}
